package com.example.provider.service.Impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.common.model.ActivityArticle;
import com.example.common.model.UserAttention;
import com.example.common.model.condition.UserAttentionCondition;
import com.example.provider.mapper.UserAttentionMapper;
import com.example.provider.service.UserAttentionService;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * 用户关注的人关系表Service接口实现
 *
 * @author 510
 * @version 1.0.0 2021-04-23
 */
@Service
@Transactional(readOnly = true)
public class UserAttentionServiceImpl extends ServiceImpl<UserAttentionMapper, UserAttention> implements UserAttentionService {
    private static final Logger logger = LoggerFactory.getLogger(UserAttentionService.class);

    @Autowired
    private UserAttentionMapper userAttentionMapper;

    @Override
    public IPage<UserAttention> findUserAttentionByCondition(UserAttentionCondition condition) {
        IPage<UserAttention> page = condition.buildPage();
        QueryWrapper<UserAttention> queryWrapper = condition.buildQueryWrapper(UserAttention.class);
        return this.page(page, queryWrapper);
    }

    //取消关注该用户
    @Transactional
    public Boolean reAttentionUser(String userId, String beNoticedUserId){
        UpdateWrapper<UserAttention> updateWrapper = new UpdateWrapper<>();
        updateWrapper
                .eq("userId", userId)
                .eq("beNoticedUserId", beNoticedUserId)
                .set("deleteFlag", 0);

        return userAttentionMapper.update(null, updateWrapper) == 1;
    }

    //关注该用户
    @Transactional
    public Boolean attentionUser(String userId, String beNoticedUserId){

        QueryWrapper<UserAttention> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId)
                    .eq("beNoticedUserId", beNoticedUserId);
        UserAttention userAttention = this.getOne(queryWrapper);
        if (userAttention != null){
            if (userAttention.getDeleteFlag() == 0){
                UpdateWrapper<UserAttention> updateWrapper = new UpdateWrapper<>();
                updateWrapper
                        .eq("userId", userId)
                        .eq("beNoticedUserId", beNoticedUserId)
                        .set("deleteFlag", 1);
                userAttentionMapper.update(null, updateWrapper);
                return true;
            }else return userAttention.getDeleteFlag() == 1;
        }
        UserAttention userAttention2 = new UserAttention();
        userAttention2.setUserId(userId);
        userAttention2.setBeNoticedUserId(beNoticedUserId);
        userAttention2.setTime(new Date());
        userAttention2.setCreationDate(new Date());
        userAttention2.setLastUpdateDate(new Date());
        userAttention2.setCreatedBy("administrator");
        userAttention2.setLastUpdatedBy("administrator");
        userAttention2.setDeleteFlag(1);

        return this.addUserAttention(userAttention2);
    }


    /**
     *  判断用户是否关注该用户
     */
    public Boolean checkUserAttention(String userId, String beNoticedUserId){
        QueryWrapper<UserAttention> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("userId", userId)
                .eq("beNoticedUserId", beNoticedUserId)
                .eq("deleteFlag", 1);
        UserAttention userAttention = this.getOne(queryWrapper);
        return userAttention != null;
    }

    @Override
    public UserAttention getUserAttentionById(Integer id) {
        return this.getById(id);
    }

    @Transactional
    @Override
    public boolean addUserAttention(UserAttention userAttention) {
        return this.save(userAttention);
    }

    @Transactional
    @Override
    public boolean updateUserAttention(UserAttention userAttention) {
        return this.updateById(userAttention);
    }

    @Transactional
    @Override
    public boolean deleteUserAttentionById(Integer id) {
        return this.removeById(id);
    }

    @Transactional
    @Override
    public boolean deleteUserAttentionByIds(List<Integer> idList) {
        return this.removeByIds(idList);
    }
}